package ru.tennisworld.user.mappers;

import org.mapstruct.BeanMapping;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.MappingTarget;
import org.mapstruct.Named;
import ru.tennisworld.user.config.MapstructConfig;
import ru.tennisworld.user.dto.profile.UserProfileRequestDto;
import ru.tennisworld.user.dto.profile.UserProfileResponseDto;
import ru.tennisworld.user.dto.profile.UserRatingReferenceDto;
import ru.tennisworld.user.entities.TennisUserProfile;

import java.time.LocalDate;
import java.util.Date;

@Mapper(config = MapstructConfig.class)
public interface TennisUserProfileMapper {

    @Named("requestProfileMapper")
    @BeanMapping(ignoreByDefault = true)
    @Mapping(target = "firstName", source = "firstName")
    @Mapping(target = "lastName", source = "lastName")
    @Mapping(target = "patronymic", source = "patronymic")
    @Mapping(target = "experience", source = "startPlayingYear", qualifiedByName = "calculateUserExperience")
    @Mapping(target = "birthday", source = "birthday")
    @Mapping(target = "rating", constant = "1000L")
    TennisUserProfile mapToEntity(UserProfileRequestDto dto);

    @Named("responseProfileMapper")
    @Mapping(target = "firstName", source = "firstName")
    @Mapping(target = "lastName", source = "lastName")
    @Mapping(target = "patronymic", source = "patronymic")
    @Mapping(target = "experience", source = "experience")
    @Mapping(target = "birthday", source = "birthday")
    @Mapping(target = "rating", source = "rating")
    UserProfileResponseDto mapToDto(TennisUserProfile entity);

    @BeanMapping(ignoreByDefault = true)
    @Mapping(target = "tennisUser")
    @Mapping(target = "experience")
    @Mapping(target = "userId")
    @Mapping(target = "firstName")
    @Mapping(target = "lastName")
    @Mapping(target = "patronymic")
    @Mapping(target = "birthday", source = "birthday")
    void updateUserProfile(TennisUserProfile srcEntity, @MappingTarget TennisUserProfile updEntity);

    @Named("calculateUserExperience")
    default Long calculateUserExperience(Long startPlayingYear) {
        return LocalDate.now().getYear() - startPlayingYear;
    }
}
